package label;

import java.util.*;

public class MatchPeptide {
	
	double delta = 0.5;
	String peptide;
	int charge;
	//ArrayList<PeakLabel> peakCL = new ArrayList<PeakLabel>();
	ArrayList<Double> bList = new ArrayList<Double>();
	ArrayList<Double> yList = new ArrayList<Double>();
	ArrayList<Double> b_iso = new ArrayList<Double>();
	ArrayList<Double> y_iso = new ArrayList<Double>();
	ArrayList<Double> b_NH3 = new ArrayList<Double>();
	ArrayList<Double> y_NH3 = new ArrayList<Double>();
	ArrayList<Double> b_H2O = new ArrayList<Double>();
	ArrayList<Double> y_H2O = new ArrayList<Double>();
	final int B_Color = 0xFF0000;			// red
	final int Iso_Color = 0xFFA500;			// orange
	final int B_NL_Color = 0xFF1493;		// deepPink
	final int Y_Color = 0x0000CD;			// mediumBlue
	final int Y_NL_Color = 0x00FF00;		// lime(lightGreen)
	
	
	public void getIonLists(String pep) {
		char c;
		char d = 'b';
		int numAnimo = pep.length();
		double mz = 0.0;
		double total = 0.0;
		
		ArrayList<Double> tempList = new ArrayList<Double>();
		tempList.clear();
		
		for (int i = 0; i < pep.length(); i++)
		{
			c = pep.charAt(i);
			if (i != pep.length() - 1) d = pep.charAt(i+1);
			
			switch(c){
			case 'G' : mz =  57.02147; break;
			case 'A' : mz =  71.03712; break;
			case 'S' : mz =  87.03203; break;
			case 'P' : mz =  97.05277; break;
			case 'V' : mz =  99.06842; break;
			case 'T' : mz = 101.04768; break;
//          case 'C' : mz = 103.02919; break;
//          case 'C' : mz = 160.02919; break;
//         	case 'C' : mz = 545.33900; break;
            case 'I' : mz = 113.08407; break;
            case 'L' : mz = 113.08407; break;

            case 'N' : mz = 114.04293; break;
            case 'D' : mz = 115.02695; break;
            case 'Q' : mz = 128.05858; break;
            case 'K' : mz = 128.09497; break;
            case 'E' : mz = 129.04260; break;
            case 'M' : mz = 131.04049; break;
            case 'H' : mz = 137.05891; break;
            case 'F' : mz = 147.06842; break;
            case 'R' : mz = 156.10112; break;
            case 'Y' : mz = 163.06333; break;
            case 'W' : mz = 186.07932; break;
            
            case '#' : break;
            case '*' : break;
            case '8' : break;
            
            default : System.out.println("found no match to animo!\n");
			}
			
			//modification
			if(d == '#') { mz = mz + 16.0; numAnimo--;}
			if(d == '*' || d == '8') { mz = mz + 9.0; numAnimo--;}
			if(c == '#' || c == '*' || c == '8') continue;
			
			total = total + mz;
			
			if (i != numAnimo - 1) {
				//b-ion need add 1.0D(one charge H+)
				double b = total + 1.0;	
				
				bList.add(b);
//				if (charge == 3)
//					byList.add((b + 1.0) / 2);	//b-ion++
				
				tempList.add(total);	//total residues in the oList	
			}	
		}
		
		
		for (int j = 0; j < numAnimo - 1; j++)
		{
			double y = total - tempList.get(numAnimo - 2 - j) + 19.0;	//y-ion put into blist and add 19.0D
			
			yList.add(y);
//			if (charge == 3)
//				byList.add((y + 1.0) / 2);	//y-ion++	
		}

//		System.out.println();
//		for (int i = 0; i < bList.size(); i++) {
//			System.out.print(bList.get(i) + "\t");
//		}
//		System.out.println();
//		for (int i = 0; i < yList.size(); i++) {
//			System.out.print(yList.get(i) + "\t");
//		}
//		System.out.println();
		
		for (int i = 0; i < bList.size(); i++) {
			double bIon = bList.get(i);
			double yIon = yList.get(i);
			
			b_iso.add(bIon + 1);
			b_NH3.add(bIon - 17);
			b_H2O.add(bIon - 18);
			
			y_iso.add(yIon + 1);
			y_NH3.add(yIon - 17);
			y_H2O.add(yIon - 18);
		}	
	}
	
	
	public int findIndex(double mz, ArrayList<Double> list) {
		
		for (int i = 0; i < list.size(); i++)
		{
			double theIon = list.get(i);
			if (mz >= theIon - delta && mz <= theIon + delta) return i;
		}
		return -1;
	}
	
	public ArrayList<PeakLabel> getMatchInfor(double[] xPeak, String pep, double tolerance) {
		
		ArrayList<PeakLabel> xPeakCL = new ArrayList<PeakLabel>();
		xPeakCL.clear();
		//peakCL.clear();	// first clear it...
		System.out.println(pep + "\t" + tolerance + "\t" + xPeak.length + "\n");
		int len = xPeak.length;
		peptide = pep;
		delta = tolerance;
		getIonLists(peptide);
//		System.out.println(" size :::: " + bList.size() + "\t" + b_iso.size() + "\t" + b_NH3.size() + "\t" + b_H2O.size() + "\t"
//								+ yList.size() + "\t" + y_iso.size() + "\n");
		
		for (int i = 0; i < len; i++) {
			PeakLabel pCL = new PeakLabel();
			pCL.mz = xPeak[i];
			
			int indexB = findIndex(pCL.mz, bList);
			int indexBiso = findIndex(pCL.mz, b_iso);			
			int indexBNH3 = findIndex(pCL.mz, b_NH3);
			int indexBH2O = findIndex(pCL.mz, b_H2O);
			int indexY = findIndex(pCL.mz, yList);
			int indexYiso = findIndex(pCL.mz, y_iso);
			int indexYNH3 = findIndex(pCL.mz, y_NH3);
			int indexYH2O = findIndex(pCL.mz, y_H2O);
			
			if (indexB != -1) {	// b_ion
				pCL.color = B_Color;
				pCL.label = "B" + (indexB+1) + "+";
				xPeakCL.add(pCL);
				continue;
			}
			if (indexY != -1) {	// y_ion
				pCL.color = Y_Color;
				pCL.label = "Y" + (indexY+1) + "+";
				xPeakCL.add(pCL);
				continue;
			}
//			if (indexBiso != -1) {
//				pCL.color = Iso_Color;
//				pCL.label = "B" + (indexBiso+1) + "iso+";
//				xPeakCL.add(pCL);
//				continue;
//			}
//			if (indexYiso != -1) {
//				pCL.color = Iso_Color;
//				pCL.label = "Y" + (indexYiso+1) + "iso+";
//				xPeakCL.add(pCL);
//				continue;
//			}
//			if (indexBNH3 != -1) {
//				pCL.color = B_NL_Color;
//				pCL.label = "B" + (indexBNH3+1) + "NH3+";
//				xPeakCL.add(pCL);
//				continue;
//			}
//			if (indexBH2O != -1) {
//				pCL.color = B_NL_Color;
//				pCL.label = "B" + (indexBH2O+1) + "H2O+";
//				xPeakCL.add(pCL);
//				continue;
//			}
//			if (indexYNH3 != -1) {
//				pCL.color = Y_NL_Color;
//				pCL.label = "Y" + (indexYNH3+1) + "NH3+";
//				xPeakCL.add(pCL);
//				continue;
//			}
//			if (indexYH2O != -1) {
//				pCL.color = Y_NL_Color;
//				pCL.label = "Y" + (indexYH2O+1) + "H2O+";
//				xPeakCL.add(pCL);
//				continue;
//			}
			pCL.color = 0x2F4F4F;	// noise peak color.
			pCL.label = " ";
			xPeakCL.add(pCL);			
		}
		
		
		bList.clear();
		b_iso.clear();
		b_NH3.clear();
		b_H2O.clear();
		yList.clear();
		y_iso.clear();
		y_NH3.clear();
		y_H2O.clear();
		
		return xPeakCL;
	}
	
	public static void main(String[] args) {
		MatchPeptide aMP = new MatchPeptide();
		aMP.getIonLists("KGITEEVAK");
	}
	
}

//class peakLabel{
//	double mz;
//	int color;
//	String label;
//}
